package org;

import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Hello world!
 *
 */
public class App 
{
    private final static String url = "http://www.cqjiazhengyun.com:9999/api/shop/cart!pollVoteInfo.do?ajax=yes";
    public static void main( String[] args )
    {
        System.out.println("即将开始模拟浏览器-伪装不同IP进行投票Version1.1");
        while(true) {
            System.out.println("投票开始");
                CloseableHttpClient hc = HttpClientBuilder.create().build();
                HttpPost post = new HttpPost(url);
                List<NameValuePair> params = new ArrayList<NameValuePair>();
                try {
                    NameValuePair nvp = new BasicNameValuePair("voteId", "1609");
                    params.add(nvp);
                    String ip = IPPackage.select();
                    //随机生成IP
                    post.addHeader("x-forwarded-for",ip);
                    post.addHeader("client-ip",ip);
                    NameValuePair nvp1 = new BasicNameValuePair("clientIp", ip);
                    params.add(nvp1);
                    NameValuePair nvp2 = new BasicNameValuePair("companyStr", "1627");
                    params.add(nvp2);
                    post.setEntity(new UrlEncodedFormEntity(params));
                    CloseableHttpResponse response = hc.execute(post);
                    if ("{\"result\":1,\"message\":\"ok\"}".equals(EntityUtils.toString(response.getEntity()))) {
                        JedisServ.put(ip,ip);
                        System.out.println("伪装IP["+ip+"]投票成功");
                    } else {
                        System.out.println("投票失败");
                    }
                }catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    int terrupt = TimeSel.build();
                    Thread.sleep(terrupt);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
